const express = require('express');

const router = express.Router();
//导入数据库操作模块
const db = require('../db/index');

router.get('/list', (req, res) => {
    //获取分类列表
    db.query('select * from category', (err, result) => {
        if (err) {
            res.json({
                code: 500,
                message: '服务器错误'
            });
        } else {
            res.json({
                code: 200,
                data: result,
                message:'获取成功'
            })
        }
    })
})

//编辑
router.post('/edit', (req, res) => {
    //获取参数
    const { id, name } = req.body;
    //判断参数是否为空
    if (!id ||!name) {
        res.json({
            code: 400,
            message: '参数不全'
        })
    } else {
        //修改
        db.query('update category set name=? where id=?', [name, id], (err, result) => {
            if (err) {
                res.json({
                    code: 500,
                    message: '服务器错误'
                });
            } else {
                res.json({
                    code: 200,
                    message: '编辑成功'
                })
            }
        })
    }
})

//新增一级分类
router.post('/addParent', (req, res) => {
    //获取参数
    const { name } = req.body;
    //判断参数是否为空
    if (!name) {
        res.json({
            code: 400,
            message: '参数不全'
        })
    } else {
        //新增
        db.query(`insert into category (name) values (?)`, [name], (err, result) => {
            if (err) {
                res.json({
                    code: 500,
                    message: '服务器错误'
                });
            } else {
                res.json({
                    code: 200,
                    message: '操作成功'
                })
            }
        }
        )
    }
})


//新增子分类
router.post('/addChild', (req, res) => {
    //获取参数
    const { id, name } = req.body;
    //判断参数是否为空
    if (!id ||!name) {
        res.json({
            code: 400,
            message: '参数不全'
        })
    } else {
        //新增
        db.query('insert into category (name, parent_id) values (?, ?)', [name, id], (err,result) => {
            if (err) {
                res.json({
                    code: 500,
                    message: '服务器错误'
                });
            } else {
                res.json({
                    code: 200,
                    message: '操作成功'
                })
            }
        })
    }
})

//删除 如果有子分类，将子分类全部删除
router.post('/delete', (req, res) => {
    const { id } = req.body;
    if (!id) {
        res.json({
            code: 400,
            message: '参数不全'
        })
    } else {
        //删除
        db.query('delete from category where id=?', [id], (err, result) => {
            if (err) {
                return res.json({
                    code: 500,
                    message: '服务器错误[1]'
                });
            } 

            //删除子分类
            db.query('delete from category where parent_id=?', [id], (err, result) => {
                if (err) {
                    res.json({
                        code: 500,
                        message: '服务器错误[2]'
                    });
                } else {
                    res.json({
                        code: 200,
                        message: '删除成功'
                    })
                }
            })
            
        })
    }
})

module.exports = router;